### Replication file for 
### "Does Chinese FDI in Africa Inspire for a China Model of Development"

### Part 3: Appendix
rm(list=ls())

### Use "Alt+O" to collapse all sections and "Shift+Alt+O" to expand all

### Set your own working directory

# Load required packages --------------------------------------------------

### packages
library(tidyverse)
library(miceadds)
library(car)
library(xtable)
library(ggplot2)
library(ggmosaic)
library(gridExtra)
library(logr) # to export the log file (optional)
library(flextable) # to export tables to Word (optional)

### suppress scientific numeric
options(scipen=999)

# Customize theme of plots ------------------------------------------------
my_theme<-theme_bw()+
  theme(panel.grid = element_blank(),
        plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"),
        legend.text = element_text(size=8),
        axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 5, l = 5),
                                   size=12,color="black"),
        axis.text.x = element_text(margin = margin(t = 0, r = 0, b = 5, l = 0),
                                   size=12,color="black",vjust = -0.5),
        axis.ticks = element_line(color="black",size=1),
        axis.ticks.length = unit(0.1, "cm"),
        axis.title.x = 
          element_text(size=12,margin = margin(t = 10, r = 0, b = 10, l = 0)),
        axis.title.y = 
          element_text(size=12,margin = margin(t = 0, r = 0, b = 5, l = 0)),
        strip.text = element_text(colour = 'black',size=18)
  )

# Load the datasets -------------------------------------------------------

### read "FDIProjGeocode.csv" into R
fdi<-read_csv("FDIProjGeocode.csv")

### load "data_respno"
load("data_respno.RData")

# Create or recode variables for FDI projects -----------------------------

### Sectors: manufacturing, resouces, and service
fdi<-fdi %>% 
  mutate(Sector_type=ifelse(Sector %in% c("Coal, oil & gas","Metals",
                                          "Minerals","Renewable energy"),
                            "Resources",
                            ifelse(Sector %in% c("Communications","Aerospace",
                                                 "Real estate","Healthcare",
                                                 "Financial services",
                                                 "Business services",
                                                 "Leisure & entertainment",
                                                 "Warehousing","Transportation",
                                                 "Software & IT services"),
                                   "Service","Manufacturing")))

### Ownership
fdi<-fdi %>% 
  mutate(Ownership_final=trimws(Ownership_final),# trim the spaces
         Ownership_final=ifelse(Ownership_final!="POE","State-owned","Private"))

### Precisely goecoded or not
fdi<-fdi %>% 
  mutate(D_precise=ifelse(Precision_final %in% c(1,2,9),"Precise","Imprecise"))

### Amount of investment (cut by quantile)
fdi<-fdi %>% 
  mutate(Amount_cut=cut_number(Amount,n=4,dig.lab=5))
levels(fdi$Amount_cut)<-c("[0.02,7.52]","(7.52,25]","(25,100]","(100,20000]")

### Year in the data (year intervals)
fdi<-fdi %>% 
  mutate(Year_cut=ifelse((Year>=2003 & Year<=2006),"2003-2006",
                         ifelse((Year>=2007 & Year<=2010),"2007-2010",
                                ifelse((Year>=2011 & Year<=2014),
                                       "2011-2014","2015-2018"))))

### Countries in both FDI and Afrobarometer datasets
country_both<-data_respno[[2]] %>% pull(countryname) %>% unique()

# Recode active, eventual, and not close ----------------------------------
for (j in 1:length(data_respno)){
  
  data_respno[[j]]<-data_respno[[j]] %>% 
    mutate(resp_status=ifelse(D_close_active_fdi==1,"Active",
                              ifelse(D_close_eventual_fdi==1,"Eventual",
                                     "Not close")),
           resp_status=factor(resp_status,
                              levels = c("Active","Eventual","Not close"))
    )
  
}

# Subsample by sector: manufacturing -------------------------------------
data_respno_manuf<-list()
for (j in 1:length(data_respno)){
  
  # Close to active or eventual manufacturing projects
  data_respno_manuf[[j]]<-data_respno[[j]] %>% 
    filter(((D_close_active_fdi==1 & Num_fdi_manuf_active>0)|
              (D_close_eventual_fdi==1 & Num_fdi_manuf_eventual>0)))
  
  # Countries included in the subsample
  country_select<-data_respno_manuf[[j]] %>% pull(countryname) %>% unique()
  
  # Respondents not close to any projects
  data_notclose<-data_respno[[j]] %>% filter(countryname %in% country_select,
                                             D_close_fdi==0)
  
  # Row bind respondents close to active or eventual and not close
  data_respno_manuf[[j]]<-rbind(data_respno_manuf[[j]],data_notclose)
  
}

# Subsample by sector: service --------------------------------------------
data_respno_service<-list()
for (j in 1:length(data_respno)){
  
  # Close to active or eventual service projects
  data_respno_service[[j]]<-data_respno[[j]] %>% 
    filter(((D_close_active_fdi==1 & Num_fdi_service_active>0)|
              (D_close_eventual_fdi==1 & Num_fdi_service_eventual>0)))
  
  # Countries included in the subsample
  country_select<-data_respno_service[[j]] %>% pull(countryname) %>% unique()
  
  # Respondents not close to any projects
  data_notclose<-data_respno[[j]] %>% filter(countryname %in% country_select,
                                             D_close_fdi==0)
  
  # Row bind respondents close to active or eventual and not close
  data_respno_service[[j]]<-rbind(data_respno_service[[j]],data_notclose)
  
}

# Subsample by sector: resource -------------------------------------------
data_respno_resource<-list()
for (j in 1:length(data_respno)){
  
  # Close to active or eventual resource projects
  data_respno_resource[[j]]<-data_respno[[j]] %>% 
    filter(((D_close_active_fdi==1 & Num_fdi_resource_active>0)|
              (D_close_eventual_fdi==1 & Num_fdi_resource_eventual>0)))
  
  # Countries included in the subsample
  country_select<-data_respno_resource[[j]] %>% pull(countryname) %>% unique()
  
  # Respondents not close to any projects
  data_notclose<-data_respno[[j]] %>% filter(countryname %in% country_select,
                                             D_close_fdi==0)
  
  # Row bind respondents close to active or eventual and not close
  data_respno_resource[[j]]<-rbind(data_respno_resource[[j]],data_notclose)
  
}

# Subsample using narrower timeframe --------------------------------------

### Time bandwidth
time_bandwidth<-c(1,2,3,4)

### A loop to create subsamples with narrower timeframes
data_respno_narrow<-rep(list(rep(list(0),length(data_respno))),
                        length(time_bandwidth))
for (t in 1:length(time_bandwidth)){
  
  for (j in 1:length(data_respno)){
    
    data_respno_narrow[[t]][[j]]<-data_respno[[j]] %>% 
      filter(!(D_close_active_fdi==1 & Duration_active_min>time_bandwidth[t]),
             !(D_close_eventual_fdi==1 & Time_eventual_min>time_bandwidth[t]))
    
  }
}

# Subsample by age group --------------------------------------------------

### Cut age into three quantiles
Age_cutoff<-data_respno[[2]] %>% 
  filter(!is.na(Age)) %>% 
  pull(Age) %>% 
  cut_number(.,n=3) %>% 
  unique() %>% 
  sort()

### Create subsamples by three age group
data_respno_age<-rep(list(rep(list(0),length(data_respno))),length(Age_cutoff))
for (i in 1:length(Age_cutoff)){
  
  for (j in 1:length(data_respno)){
    
    data_respno_age[[i]][[j]]<-data_respno[[j]] %>% 
      filter(!is.na(Age)) %>% 
      mutate(Age_level=cut_number(Age,n=3)) %>% 
      filter(Age_level==Age_cutoff[i])
    
  }
  
}



# Subsample by capital city -----------------------------------------------

### Create subsample close to projects in capital cities
data_respno_capital<-list()
for (j in 1:length(data_respno)){
  
  # Close to active or eventual projects in capital cities
  data_respno_capital[[j]]<-data_respno[[j]] %>% 
    filter(((D_close_active_fdi==1 & Num_fdi_capital_active>0)|
              (D_close_eventual_fdi==1 & Num_fdi_capital_eventual>0)))
  
  # Countries included in the subsample
  country_select<-data_respno_capital[[j]] %>% pull(countryname) %>% unique()
  
  # Respondents not close to any projects
  data_notclose<-data_respno[[j]] %>% filter(countryname %in% country_select,
                                             D_close_fdi==0)
  
  # Row bind respondents close to active or eventual and not close
  data_respno_capital[[j]]<-rbind(data_respno_capital[[j]],data_notclose)
  
}

### Create subsamples close to projects not in capital cities
data_respno_notcapital<-list()
for (j in 1:length(data_respno)){
  
  # Close to active or eventual projects in capital cities
  data_respno_notcapital[[j]]<-data_respno[[j]] %>% 
    filter(((D_close_active_fdi==1 & Num_fdi_capital_active==0)|
              (D_close_eventual_fdi==1 & Num_fdi_capital_eventual==0)))
  
  # Countries included in the subsample
  country_select<-data_respno_notcapital[[j]] %>% pull(countryname) %>% unique()
  
  # Respondents not close to any projects
  data_notclose<-data_respno[[j]] %>% filter(countryname %in% country_select,
                                             D_close_fdi==0)
  
  # Row bind respondents close to active or eventual and not close
  data_respno_notcapital[[j]]<-rbind(data_respno_notcapital[[j]],data_notclose)
  
}

# Subsample of countries with both active and eventual --------------------

### Create subsamples restricting to countries with both active and eventual
data_respno_subctry<-list()
for (j in 1:length(data_respno)){
  
  tab<-table(data_respno[[j]]$countryname,data_respno[[j]]$resp_status)
  drop<-(tab[,1]==0|tab[,2]==0|tab[,3]==0)
  drop_country<-rownames(tab)[drop]
  
  data_respno_subctry[[j]]<-data_respno[[j]] %>% 
    filter(!countryname %in% drop_country)
}

### Create subsamples dropping not close
data_respno_subctry_close<-list()
for (j in 1:length(data_respno)){
  data_respno_subctry_close[[j]]<-data_respno_subctry[[j]] %>% 
    filter(resp_status!="Not close")
}

# Function: country FE and table ------------------------------------------
func_model<-function(d,dv){
  
  m<-lm.cluster(data=d,
                # cluster the standard error in the survey cluster
                cluster=d$uniqueea,
                # dependent variables and status to Chinese FDI
                d[,dv]~D_close_active_fdi+D_close_eventual_fdi
                # control variables
                +as.factor(Urban)+Age+I(Age^2)+as.factor(Gender)+
                  as.factor(Education)
                # country fixed effects
                +as.factor(countryname)
  )
  
  # Coefficients of interests (Active and Eventual)
  coefs<-unname(coef(m)[2:3])
  
  # t values
  tvalue<-unname(summary(m)[2:3,3])
  
  # Active-Eventual
  diff<-coefs[1]-coefs[2]
  test_diff<-linearHypothesis(m,"D_close_active_fdi=D_close_eventual_fdi")
  F_diff<-test_diff$Chisq[2]
  p_diff<-test_diff$`Pr(>Chisq)`[2]
  
  # Number of countries
  N_ctry<-d %>% 
    pull(countryname) %>% 
    unique() %>% 
    length()
  
  # Number of enumeration areas
  N_ea<-d %>% 
    subset(select=c(dv,"D_close_active_fdi","D_close_eventual_fdi",
                    "Urban","Age","Gender","Education","uniqueea")) %>% 
    na.omit() %>% 
    pull(uniqueea) %>% 
    unique() %>% 
    length()
  
  # Number of observations
  N_obs<-d %>% 
    subset(select=c(dv,"D_close_active_fdi","D_close_eventual_fdi",
                    "Urban","Age","Gender","Education")) %>% 
    na.omit() %>% 
    nrow()
  
  # Combine the results
  out<-c(coefs[1],tvalue[1],coefs[2],tvalue[2],diff,F_diff,p_diff,NA,NA,
         N_ctry,N_ea,N_obs,summary(m$lm_res)$adj.r.squared)
  print(out)
  
} 

# Function: subnational FE and table --------------------------------------
func_model_regionFE<-function(d,dv){
  
  m<-lm.cluster(data=d,
                # cluster the standard error in the survey cluster
                cluster=d$uniqueea,
                # dependent variables and status to Chinese FDI
                d[,dv]~D_close_active_fdi+D_close_eventual_fdi
                # control variables
                +as.factor(Urban)+Age+I(Age^2)+as.factor(Gender)+
                  as.factor(Education)
                # country fixed effects
                +as.factor(region)
  )
  
  # Coefficients of interests (Active and Eventual)
  coefs<-unname(coef(m)[2:3])
  
  # t values
  tvalue<-unname(summary(m)[2:3,3])
  
  # Active-Eventual
  diff<-coefs[1]-coefs[2]
  test_diff<-linearHypothesis(m,"D_close_active_fdi=D_close_eventual_fdi")
  F_diff<-test_diff$Chisq[2]
  p_diff<-test_diff$`Pr(>Chisq)`[2]
  
  # Number of subnational regions
  N_region<-d %>% 
    pull(region) %>% 
    unique() %>% 
    length()
  
  # Number of enumeration areas
  N_ea<-d %>% 
    subset(select=c(dv,"D_close_active_fdi","D_close_eventual_fdi",
                    "Urban","Age","Gender","Education","uniqueea")) %>% 
    na.omit() %>% 
    pull(uniqueea) %>% 
    unique() %>% 
    length()
  
  # Number of observations
  N_obs<-d %>% 
    subset(select=c(dv,"D_close_active_fdi","D_close_eventual_fdi",
                    "Urban","Age","Gender","Education")) %>% 
    na.omit() %>% 
    nrow()
  
  # Combine the results
  out<-c(coefs[1],tvalue[1],coefs[2],tvalue[2],diff,F_diff,p_diff,NA,NA,
         N_region,N_ea,N_obs,summary(m$lm_res)$adj.r.squared)
  print(out)
  
} 

# Function: excluding not close and table ---------------------------------
func_model_close<-function(d,dv){
  
  m<-lm.cluster(data=d,
                # cluster the standard error in the survey cluster
                cluster=d$uniqueea,
                # dependent variables and status to Chinese FDI
                d[,dv]~D_close_active_fdi
                # control variables
                +as.factor(Urban)+Age+I(Age^2)+as.factor(Gender)+
                  as.factor(Education)
                # country fixed effects
                +as.factor(countryname)
  )
  
  # Coefficients of interests (Active and Eventual)
  coefs<-unname(coef(m)[2])
  
  # t values
  tvalue<-unname(summary(m)[2,3])
  
  # Number of countries
  N_ctry<-d %>% 
    pull(countryname) %>% 
    unique() %>% 
    length()
  
  # Number of enumeration areas
  N_ea<-d %>% 
    subset(select=c(dv,"D_close_active_fdi",
                    "Urban","Age","Gender","Education","uniqueea")) %>% 
    na.omit() %>% 
    pull(uniqueea) %>% 
    unique() %>% 
    length()
  
  # Number of observations
  N_obs<-d %>% 
    subset(select=c(dv,"D_close_active_fdi",
                    "Urban","Age","Gender","Education")) %>% 
    na.omit() %>% 
    nrow()
  
  # Combine the results
  out<-c(coefs[1],tvalue[1],NA,NA,
         N_ctry,N_ea,N_obs,summary(m$lm_res)$adj.r.squared)
  print(out)
  
} 

# Function: making main regression tables ---------------------------------
func_table<-function(out_list){
  
  out_table<-do.call(cbind,out_list)
  
  out_table[-c(10:12),]<-format(round(out_table[-c(10:12),],3),nsmall=3)
  out_table<-trimws(out_table)
  out_table[8:9,]<-"Yes"
  out_table[c(2,4),]<-paste("(",out_table[c(2,4),],")",sep="")
  
  names_iv<-c("Active","","Eventual","","Active-Eventual",
              "F test: Active-Eventual=0","p value",
              "Individual Controls","Country fixed effects",
              "Number of countries","Number of enumeration areas",
              "Number of observations","Adjusted R squared")
  
  out_table<-cbind(names_iv,out_table)
  
}

# Precisely and imprecisely geocoded projects: Figure A1 --------------

### Panel (a) of Figure A1: sector 
p_sector<-fdi %>% 
  filter(Source_country=="China",Project_type=="New") %>% 
  mutate(Sector_type=factor(Sector_type,levels=c("Service","Resources",
                                                 "Manufacturing")),
         D_precise=factor(D_precise,levels=c("Precise","Imprecise"))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(D_precise),fill=Sector_type))+
  xlab("(a) Sector")+ylab("")+
  scale_fill_manual(values=c("gray40","gray60","gray80"),guide=F)+
  my_theme

### Panel (b) of Figure A1: ownership type
p_ownership<-fdi %>% 
  filter(Source_country=="China",Project_type=="New") %>% 
  mutate(D_precise=factor(D_precise,levels=c("Precise","Imprecise"))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(D_precise),fill=Ownership_final))+
  xlab("(b) Ownership")+ylab("")+
  scale_fill_manual(values=c("gray40","gray60"),guide=F)+
  my_theme

### Panel (c) of Figure A1: year in the dataset
p_year<-fdi %>% 
  filter(Source_country=="China",Project_type=="New") %>% 
  mutate(D_precise=factor(D_precise,levels=c("Precise","Imprecise")),
         Year_cut=factor(Year_cut,levels=c("2015-2018","2011-2014",
                                           "2007-2010","2003-2006"))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(D_precise),fill=Year_cut))+
  xlab("(c) Year")+ylab("")+
  scale_fill_manual(values=c("gray20","gray40","gray60","gray80"),guide=F)+
  my_theme

### Panel (d) of Figure A1: amount of investment
p_amount<-fdi %>% 
  filter(Source_country=="China",Project_type=="New",!is.na(Amount_cut)) %>% 
  mutate(D_precise=factor(D_precise,levels=c("Precise","Imprecise")),
         Amount_cut=factor(Amount_cut,levels=c("(100,20000]","(25,100]",
                                               "(7.52,25]","[0.02,7.52]"))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(D_precise),fill=Amount_cut))+
  xlab("(d) Investment amount (US$m)")+ylab("")+
  scale_fill_manual(values=c("gray20","gray40","gray60","gray80"),guide=F)+
  my_theme

### Panel (e) of Figure A1: country
p_country<-fdi %>% 
  filter(Source_country=="China",Destination_country %in% country_both,
         Project_type=="New") %>% 
  count(Destination_country,D_precise) %>% 
  mutate(D_precise=factor(D_precise,levels=c("Precise","Imprecise"))) %>%
  ggplot()+
  geom_bar(aes(x=reorder(Destination_country,n),y=n,fill=D_precise),
           stat = "identity")+
  scale_fill_manual(values=c("gray40","gray60"),
                    guide=guide_legend(title=NULL))+
  ylab("(e) Country")+xlab("")+
  coord_flip()+
  my_theme+
  theme(legend.position = c(0.75,0.2),
        axis.text.y = element_text(size=8))

### Print Figure A1
windows(width = 800,height = 450)
p_A1<-grid.arrange(p_sector,p_ownership,p_year,p_amount,p_country,ncol=3)
p_A1
# ggsave("Figure_A1 precise and imprecise.tiff",plot=p_A1)

# Support for the China model by country: Figure A2 ----------------------

### Print Figure A2
windows(width=600,height=500)
data_respno[[2]] %>% 
  group_by(countryname) %>% 
  mutate(ChinaModel_percent=100*mean(BestModel_China,na.rm=T)) %>%
  subset(select=c(countryname,ChinaModel_percent)) %>%
  unique() %>% 
  ggplot()+
  geom_bar(aes(y=reorder(countryname,ChinaModel_percent),x=ChinaModel_percent),
           stat = "identity",fill="gray60")+
  ylab("")+xlab("China as the best model of development (%)")+
  scale_x_continuous(limits=c(0,80))+
  my_theme

### Save Figure A2
ggsave("Figure_A2 support for China model by country.tiff")

# Balance across active, eventual, and not close: Figure A3 --------------

### Panel (a) of Figure A3: urban
p_urban<-data_respno[[2]] %>% 
  mutate(Urban_level=factor(Urban_level,levels=rev(levels(Urban_level)))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(resp_status),fill=Urban_level))+
  xlab("(a) Urban")+ylab("")+
  scale_fill_manual(values=c("gray40","gray60","gray80"),guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=10),
        axis.text.y = element_text(size=10))

### Panel (b) of Figure A3: education
p_edu<-data_respno[[2]] %>% 
  filter(!is.na(Education_level)) %>% 
  mutate(Education_level=factor(Education_level,
                                levels=rev(levels(Education_level)))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(resp_status),fill=Education_level))+
  xlab("(b) Education")+ylab("")+
  scale_fill_manual(values=c("gray10","gray20","gray30","gray40","gray50",
                             "gray60","gray70","gray75","gray80","gray90"),
                    guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=8),
        axis.text.y = element_text(size=8))

### Panel (c) of Figure A3: gender
p_gender<-data_respno[[2]] %>% 
  ggplot()+
  geom_mosaic(aes(x=product(resp_status),fill=Gender_level))+
  xlab("(c) Gender")+ylab("")+
  scale_fill_manual(values=c("gray60","gray80"),
                    guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=10),
        axis.text.y = element_text(size=10))

### Panel (d) of Figure A3: age
p_age<-data_respno[[2]] %>% 
  filter(!is.na(Age_cut)) %>% 
  mutate(Age_cut=factor(Age_cut,levels=rev(levels(Age_cut)))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(resp_status),fill=Age_cut))+
  xlab("(d) Age")+ylab("")+
  scale_fill_manual(values=c("gray20","gray40","gray60","gray80"),
                    guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=10),
        axis.text.y = element_text(size=10))

### Print Figure A3
windows(width = 800,height = 450)
p_A3<-grid.arrange(p_urban,p_edu,p_gender,p_age,ncol=2)
p_A3
# ggsave("Figure_A3 balance on observables.tiff",plot=p_A3)

# Type of FDI projects by country: Table A3 ------------------------------

### All projects
all_sector_ctry<-fdi %>% 
  filter(Source_country=="China",Project_type=="New",
         Destination_country %in% country_both) %>% 
  subset(select=c(Destination_country,Sector_type)) %>% 
  table()

### Precisely geocoded projects
precise_sector_ctry<-fdi %>% 
  filter(Source_country=="China",Project_type=="New",
         Destination_country %in% country_both,
         Precision_final %in% c(1,2,9)) %>% 
  subset(select=c(Destination_country,Sector_type)) %>% 
  table()

### Print Table A3
Table_A3<-cbind(rownames(all_sector_ctry),all_sector_ctry,precise_sector_ctry)
colnames(Table_A3)<-c("","Manufacturing_all","Resources_all","Service_all",
                      "Manufacturing_precise","Resources_precise",
                      "Service_precise")
Table_A3

### Print the log file for Table A3
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_A3.log")
# log_print(Table_A3)
# log_close()

### Save Table A.3 as a Word document
# Table_A3_doc<-flextable(as.data.frame(Table_A3))
# save_as_docx(Table_A3_doc,path = "C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_A3.docx")

# Number of active, eventual, and not close by country: Table A6 ---------

### active, eventual and not close by country (25km)
status_25<-data_respno[[1]] %>% 
  subset(select=c(countryname,resp_status)) %>% 
  table()

### active, eventual and not close by country (50km)
status_50<-data_respno[[2]] %>% 
  subset(select=c(countryname,resp_status)) %>% 
  table()

### Print Table A.6
Table_A6<-cbind(rownames(status_25),status_25,status_50)
colnames(Table_A6)<-c("Country","Active_25","Eventual_25","Notclose_25",
                      "Active_50","Eventual_50","Notclose_50")
Table_A6

### Print the log file for Table A6
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_A6.log")
# log_print(Table_A6)
# log_close()

### Save Table A.6 as a Word document
# Table_A6_doc<-flextable(as.data.frame(Table_A6))
# save_as_docx(Table_A6_doc,path = "C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_A6 resp by country.docx")

# Crosstab between China model and China's image: Table A7 ---------------

### Print Table A.7
Table_A7<-data_respno[[2]] %>% 
  subset(select=c(BestModel_China,Positive_D)) %>% 
  table() %>% 
  prop.table(.,1)
Table_A7<-Table_A7*100
Table_A7<-format(round(Table_A7,3),nsmall=3)
colnames(Table_A7)<-c("Negative, neutral, and don't know",
                      "Somewhat or very positive")
Table_A7<-cbind(c("China not as the best model","China as the best model"),
                Table_A7)
Table_A7

### Print the log file for Table A7
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_A7.log")
# log_print(Table_A7)
# log_close()

### Save Table A.7 as a Word document
# Table_A7_doc<-flextable(as.data.frame(Table_A7))
# save_as_docx(Table_A7_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_A7 crosstab.docx")

# Main results by sector: Table B1 to B3 --------------------------------

### main dependent variables
dv_main<-c("BestModel_China","Positive_O","Positive_D")

### number of distance buffers
num_distance<-2

### data by sector (25km and 50km)
data_sector<-list(data_respno_manuf[1:num_distance],
                  data_respno_resource[1:num_distance],
                  data_respno_service[1:num_distance])

### Estimate the effects and output tables
out_main_sector<-rep(list(rep(list(rep(list(0),length(dv_main))),num_distance)),
                     length(data_sector))
for (i in 1:length(data_sector)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_main)){
      
      out_main_sector[[i]][[j]][[k]]<-func_model(data_sector[[i]][[j]],
                                                 dv_main[k])
      
    }
  }
}

### Print Table B.1: main results for manufacturing projects
Table_B1<-func_table(c(out_main_sector[[1]][[1]],out_main_sector[[1]][[2]]))
colnames(Table_B1)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_B1

### Print the log file for Table B1
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_B1.log")
# log_print(Table_B1)
# log_close()

### Save Table B.1 as a Word document
# Table_B1_doc<-flextable(as.data.frame(Table_B1))
# save_as_docx(Table_B1_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_B1.docx")

### Print Table B.2: main results for resources projects
Table_B2<-func_table(c(out_main_sector[[2]][[1]],out_main_sector[[2]][[2]]))
colnames(Table_B2)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_B2

### Print the log file for Table B2
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_B2.log")
# log_print(Table_B2)
# log_close()

### Save Table B.2 as a Word document
# Table_B2_doc<-flextable(as.data.frame(Table_B2))
# save_as_docx(Table_B2_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_B2.docx")

### Print Table B.3: main results for service projects
Table_B3<-func_table(c(out_main_sector[[3]][[1]],out_main_sector[[3]][[2]]))
colnames(Table_B3)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_B3

### Print the log file for Table B3
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_B3.log")
# log_print(Table_B3)
# log_close()

### Save Table B.3 as a Word document
# Table_B3_doc<-flextable(as.data.frame(Table_B3))
# save_as_docx(Table_B3_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_B3.docx")

# Positive factors: Table C1 to C4 --------------------------------------

### Positive factors
dv_positive<-c("Business_Positive","Infrastructure_Positive",
               "ProductCost_Positive","People_Positive")

### dataset (25km and 50km)
data_list<-list(data_respno[1:num_distance],
                data_respno_manuf[1:num_distance],
                data_respno_resource[1:num_distance],
                data_respno_service[1:num_distance])

### Estimate effects and output tables
out_positive<-rep(list(rep(list(rep(list(0),length(dv_positive))),num_distance)),
                  length(data_list))
for (i in 1:length(data_list)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_positive)){
      
      out_positive[[i]][[j]][[k]]<-func_model(data_list[[i]][[j]],
                                              dv_positive[k])
      
    }
  }
}

### Print Table C.1: positive factors
Table_C1<-func_table(c(out_positive[[1]][[1]],out_positive[[1]][[2]]))
colnames(Table_C1)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_C1

### Print the log file for Table C1
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C1.log")
# log_print(Table_C1)
# log_close()

### Save Table C.1 as a Word document
# Table_C1_doc<-flextable(as.data.frame(Table_C1))
# save_as_docx(Table_C1_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C1.docx")

### Print Table C.2: positive factors manufacturing
Table_C2<-func_table(c(out_positive[[2]][[1]],out_positive[[2]][[2]]))
colnames(Table_C2)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_C2

### Print the log file for Table C2
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C2.log")
# log_print(Table_C2)
# log_close()

### Save Table C.2 as a Word document
# Table_C2_doc<-flextable(as.data.frame(Table_C2))
# save_as_docx(Table_C2_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C2.docx")

### Print Table C.3: positive factors resources
Table_C3<-func_table(c(out_positive[[3]][[1]],out_positive[[3]][[2]]))
colnames(Table_C3)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_C3

### Print the log file for Table C3
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C3.log")
# log_print(Table_C3)
# log_close()

### Save Table C.3 as a Word document
# Table_C3_doc<-flextable(as.data.frame(Table_C3))
# save_as_docx(Table_C3_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C3.docx")

### Print Table C.4: positive factors service
Table_C4<-func_table(c(out_positive[[4]][[1]],out_positive[[4]][[2]]))
colnames(Table_C4)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_C4

### Print the log file for Table C4
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C4.log")
# log_print(Table_C4)
# log_close()

### Save Table C.4 as a Word document
# Table_C4_doc<-flextable(as.data.frame(Table_C4))
# save_as_docx(Table_C4_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_C4.docx")

# Negative factors: Table D1 to D4 --------------------------------------

### Negative factors
dv_negative<-c("JobBusiness_Negative","Land_Negative","Extract_Negative",
               "ProductQuality_Negative","People_Negative")

### Estimate effects and output tables
out_negative<-rep(list(rep(list(rep(list(0),length(dv_negative))),num_distance)),
                  length(data_list))
for (i in 1:length(data_list)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_negative)){
      
      out_negative[[i]][[j]][[k]]<-func_model(data_list[[i]][[j]],
                                              dv_negative[k])
      
    }
  }
}

### Print Table D.1: negative factors
Table_D1<-func_table(c(out_negative[[1]][[1]],out_negative[[1]][[2]]))
colnames(Table_D1)<-c("","(1)","(2)","(3)","(4)","(5)",
                      "(6)","(7)","(8)","(9)","(10)")
Table_D1

### Print the log file for Table D1
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D1.log")
# log_print(Table_D1)
# log_close()

### Save Table D.1 as a Word document
# Table_D1_doc<-flextable(as.data.frame(Table_D1))
# save_as_docx(Table_D1_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D1.docx")

### Print Table D.2: negative factors manufacturing
Table_D2<-func_table(c(out_negative[[2]][[1]],out_negative[[2]][[2]]))
colnames(Table_D2)<-c("","(1)","(2)","(3)","(4)","(5)",
                      "(6)","(7)","(8)","(9)","(10)")
Table_D2

### Print the log file for Table D2
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D2.log")
# log_print(Table_D2)
# log_close()

### Save Table D.2 as a Word document
# Table_D2_doc<-flextable(as.data.frame(Table_D2))
# save_as_docx(Table_D2_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D2.docx")

### Print Table D.3: negative factors resources
Table_D3<-func_table(c(out_negative[[3]][[1]],out_negative[[3]][[2]]))
colnames(Table_D3)<-c("","(1)","(2)","(3)","(4)","(5)",
                      "(6)","(7)","(8)","(9)","(10)")
Table_D3

### Print the log file for Table D3
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D3.log")
# log_print(Table_D3)
# log_close()

### Save Table D.3 as a Word document
# Table_D3_doc<-flextable(as.data.frame(Table_D3))
# save_as_docx(Table_D3_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D3.docx")

### Print Table D.4: negative factors service
Table_D4<-func_table(c(out_negative[[4]][[1]],out_negative[[4]][[2]]))
colnames(Table_D4)<-c("","(1)","(2)","(3)","(4)","(5)",
                      "(6)","(7)","(8)","(9)","(10)")
Table_D4

### Print the log file for Table D4
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D4.log")
# log_print(Table_D4)
# log_close()

### Save Table D.4 as a Word document
# Table_D4_doc<-flextable(as.data.frame(Table_D4))
# save_as_docx(Table_D4_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_D4.docx")

# Robustness subnational region FE: Table E1 -----------------------------

### Estimate effects and output tables
out_main_regionFE<-rep(list(rep(list(0),length(dv_main))),num_distance)
for (j in 1:num_distance){
  
  for (k in 1:length(dv_main)){
    
    out_main_regionFE[[j]][[k]]<-func_model_regionFE(data_respno[[j]],
                                                       dv_main[k])
    
  }
}

### Print Table E.1
Table_E1<-func_table(c(out_main_regionFE[[1]],out_main_regionFE[[2]]))
Table_E1[9,1]<-"Subnational region fixed effects"
Table_E1[10,1]<-"Number of subnational regions"
colnames(Table_E1)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E1

### Print the log file for Table E1
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E1.log")
# log_print(Table_E1)
# log_close()

### Save Table E.1 as a Word document
# Table_E1_doc<-flextable(as.data.frame(Table_E1))
# save_as_docx(Table_E1_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E1.docx")

# Robustness narrower time frames: Figure E1 and Table E2-5 -------------

### Panel (a) of Figure E.1: sector over year
p_sector_year<-fdi %>% 
  filter(Source_country=="China",Project_type=="New",
         Precision_final %in% c(1,2,9)) %>% 
  mutate(Sector_type=factor(Sector_type,levels=c("Service","Resources",
                                                 "Manufacturing"))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(Year_cut),fill=Sector_type))+
  xlab("(a) Sector")+ylab("")+
  scale_fill_manual(values=c("gray40","gray60","gray80"),guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=10),
        axis.text.y = element_text(size=10))

### Panel (b) of Figure E.1: investment amount over year
p_amount_year<-fdi %>% 
  filter(Source_country=="China",Project_type=="New",
         Precision_final %in% c(1,2,9),
         !is.na(Amount_cut)) %>% 
  mutate(Amount_cut=factor(Amount_cut,levels=rev(levels(Amount_cut)))) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(Year_cut),fill=Amount_cut))+
  xlab("(b) Investment Amount (US$m)")+ylab("")+
  scale_fill_manual(values=c("gray20","gray40","gray60","gray80"),guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=10),
        axis.text.y = element_text(size=10))

### Panel (c) of Figure E.1: ownership over year
p_ownership_year<-fdi %>% 
  filter(Source_country=="China",Project_type=="New",
         Precision_final %in% c(1,2,9)) %>% 
  ggplot()+
  geom_mosaic(aes(x=product(Year_cut),fill=Ownership_final))+
  xlab("(c) Ownership")+ylab("")+
  scale_fill_manual(values=c("gray40","gray60"),guide=F)+
  my_theme+
  theme(axis.text.x  = element_text(size=10),
        axis.text.y = element_text(size=10))

### Print Figure E.1
windows(width = 800,height = 450)
p_E1<-grid.arrange(p_sector_year,p_amount_year,p_ownership_year,ncol=1)
p_E1
# ggsave("Figure_E1 early and late projects.tiff",plot=p_E1)

### data with narrower timeframes
data_narrower<-list(data_respno_narrow[[1]][1:num_distance],
                    data_respno_narrow[[2]][1:num_distance],
                    data_respno_narrow[[3]][1:num_distance],
                    data_respno_narrow[[4]][1:num_distance])

### Estimate effects and output tables
out_main_narrower<-rep(list(rep(list(rep(list(0),length(dv_main))),
                                num_distance)),length(data_narrower))
for (i in 1:length(data_narrower)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_main)){
      
      out_main_narrower[[i]][[j]][[k]]<-func_model(data_narrower[[i]][[j]],
                                                   dv_main[k])
      
    }
  }
}

### Print Table E.2: main results active and eventual within 4 years
Table_E2<-func_table(c(out_main_narrower[[4]][[1]],out_main_narrower[[4]][[2]]))
colnames(Table_E2)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E2

### Print the log file for Table E2
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E2.log")
# log_print(Table_E2)
# log_close()

### Save Table E.2 as a Word document
# Table_E2_doc<-flextable(as.data.frame(Table_E2))
# save_as_docx(Table_E2_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E2.docx")

### Print Table E.3: main results active and eventual within 3 years
Table_E3<-func_table(c(out_main_narrower[[3]][[1]],out_main_narrower[[3]][[2]]))
colnames(Table_E3)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E3

### Print the log file for Table E3
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E3.log")
# log_print(Table_E3)
# log_close()

### Save Table E.3 as a Word document
# Table_E3_doc<-flextable(as.data.frame(Table_E3))
# save_as_docx(Table_E3_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E3.docx")

### Print Table E.4: main results active and eventual within 2 years
Table_E4<-func_table(c(out_main_narrower[[2]][[1]],out_main_narrower[[2]][[2]]))
colnames(Table_E4)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E4

### Print the log file for Table E4
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E4.log")
# log_print(Table_E4)
# log_close()


### Save Table E.4 as a Word document
# Table_E4_doc<-flextable(as.data.frame(Table_E4))
# save_as_docx(Table_E4_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E4.docx")

### Print Table E.5: main results active and eventual within 1 years
Table_E5<-func_table(c(out_main_narrower[[1]][[1]],out_main_narrower[[1]][[2]]))
colnames(Table_E5)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E5

### Print the log file for Table E5
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E5.log")
# log_print(Table_E5)
# log_close()

### Save Table E.5 as a Word document
# Table_E5_doc<-flextable(as.data.frame(Table_E5))
# save_as_docx(Table_E5_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E5.docx")

# Robustness countries with both active and eventual: Table E6-7 ---------

### Estimate effects and output tables
out_main_subctry<-rep(list(rep(list(0),length(dv_main))),num_distance)
for (j in 1:num_distance){
  for (k in 1:length(dv_main)){
      
      out_main_subctry[[j]][[k]]<-func_model(data_respno_subctry[[j]],
                                             dv_main[k])
      
  }
}

### Print Table E.6: main results countries with both active and eventual
Table_E6<-func_table(c(out_main_subctry[[1]],out_main_subctry[[2]]))
colnames(Table_E6)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E6

### Print the log file for Table E6
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E6.log")
# log_print(Table_E6)
# log_close()

### Save Table E.6 as a Word document
# Table_E6_doc<-flextable(as.data.frame(Table_E6))
# save_as_docx(Table_E6_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E6.docx")

### Estimate effects and output tables (exclude not close)
out_main_subctry_close<-rep(list(rep(list(0),length(dv_main))),num_distance)
for (j in 1:num_distance){
  for (k in 1:length(dv_main)){
    
    out_main_subctry_close[[j]][[k]]<-
      func_model_close(data_respno_subctry_close[[j]],dv_main[k])
    
  }
}

### Print Table E.7: main results countries excluding not close
Table_E7<-do.call(cbind,c(out_main_subctry_close[[1]],
                          out_main_subctry_close[[2]]))
Table_E7[-c(5:7),]<-format(round(Table_E7[-c(5:7),],3),nsmall=3)
Table_E7<-trimws(Table_E7)
Table_E7[3:4,]<-"Yes"
Table_E7[2,]<-paste("(",Table_E7[2,],")",sep="")
names_iv<-c("Active","",
            "Individual Controls","Country fixed effects",
            "Number of countries","Number of enumeration areas",
            "Number of observations","Adjusted R squared")
Table_E7<-cbind(names_iv,Table_E7)
colnames(Table_E7)<-c("","(1)","(2)","(3)","(4)","(5)","(6)")
Table_E7

### Print the log file for Table E7
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E7.log")
# log_print(Table_E7)
# log_close()

### Save Table E.7 as a Word document
# Table_E7_doc<-flextable(as.data.frame(Table_E7))
# save_as_docx(Table_E7_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E7.docx")

# Robustness projects in capital cities: Table E8 ------------------------

### dataset by capital regions
data_capital<-list(data_respno_capital[1:num_distance],
                   data_respno_notcapital[1:num_distance])

### Estimate effects and output tables
out_main_capital<-rep(list(rep(list(rep(list(0),length(dv_main))),
                                num_distance)),length(data_capital))
for (i in 1:length(data_capital)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_main)){
      
      out_main_capital[[i]][[j]][[k]]<-func_model(data_capital[[i]][[j]],
                                                  dv_main[k])
      
    }
  }
}

### Print Table E.8: main results projects in capital cities or not
Table_E8<-func_table(list(out_main_capital[[1]][[1]][[1]],
                          out_main_capital[[1]][[2]][[1]],
                          out_main_capital[[2]][[1]][[1]],
                          out_main_capital[[2]][[2]][[1]]))
colnames(Table_E8)<-c("","(1)","(2)","(3)","(4)")
Table_E8

### Print the log file for Table E8
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E8.log")
# log_print(Table_E8)
# log_close()

### Save Table E.8 as a word document
# Table_E8_doc<-flextable(as.data.frame(Table_E8))
# save_as_docx(Table_E8_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E8.docx")

# Robustness positive and negative factors by age: Table E9-14 --------

### dataset (25km and 50km)
data_age<-list(data_respno_age[[1]][1:num_distance],
               data_respno_age[[2]][1:num_distance],
               data_respno_age[[3]][1:num_distance])

### Estimate effects and output tables (positive)
out_positive_age<-rep(list(rep(list(rep(list(0),length(dv_positive))),
                               num_distance)),length(data_age))
for (i in 1:length(data_age)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_positive)){
      
      out_positive_age[[i]][[j]][[k]]<-func_model(data_age[[i]][[j]],
                                                  dv_positive[k])
      
    }
  }
}

### Print Table E.9: positive factors age [18,28]
Table_E9<-func_table(c(out_positive_age[[1]][[1]],out_positive_age[[1]][[2]]))
colnames(Table_E9)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_E9

### Print the log file for Table E9
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E9.log")
# log_print(Table_E9)
# log_close()

### Save Table E.9 as a Word document
# Table_E9_doc<-flextable(as.data.frame(Table_E9))
# save_as_docx(Table_E9_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E9.docx")

### Print Table E.10: positive factors age [29,41]
Table_E10<-func_table(c(out_positive_age[[2]][[1]],out_positive_age[[2]][[2]]))
colnames(Table_E10)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_E10

### Print the log file for Table E10
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E10.log")
# log_print(Table_E10)
# log_close()

### Save Table E.10 as a Word document
# Table_E10_doc<-flextable(as.data.frame(Table_E10))
# save_as_docx(Table_E10_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E10.docx")

### Print Table E.11: positive factors age 42 and above
Table_E11<-func_table(c(out_positive_age[[3]][[1]],out_positive_age[[3]][[2]]))
colnames(Table_E11)<-c("","(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)")
Table_E11

### Print the log file for Table E11
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E11.log")
# log_print(Table_E11)
# log_close()

### Save Table E.11 as a Word document
# Table_E11_doc<-flextable(as.data.frame(Table_E11))
# save_as_docx(Table_E11_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E11.docx")

### Estimate effects and output tables (negative)
out_negative_age<-rep(list(rep(list(rep(list(0),length(dv_negative))),
                               num_distance)),length(data_age))
for (i in 1:length(data_age)){
  for (j in 1:num_distance){
    for (k in 1:length(dv_negative)){
      
      out_negative_age[[i]][[j]][[k]]<-func_model(data_age[[i]][[j]],
                                                  dv_negative[k])
      
    }
  }
}

### Print Table E.12: negative factors age [18,28]
Table_E12<-func_table(c(out_negative_age[[1]][[1]],out_negative_age[[1]][[2]]))
colnames(Table_E12)<-c("","(1)","(2)","(3)","(4)","(5)",
                       "(6)","(7)","(8)","(9)","(10)")
Table_E12

### Print the log file for Table E12
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E12.log")
# log_print(Table_E12)
# log_close()

### Save Table E.12 as a Word document
# Table_E12_doc<-flextable(as.data.frame(Table_E12))
# save_as_docx(Table_E12_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E12.docx")

### Print Table E.13: negative factors age [29,41]
Table_E13<-func_table(c(out_negative_age[[2]][[1]],out_negative_age[[2]][[2]]))
colnames(Table_E13)<-c("","(1)","(2)","(3)","(4)","(5)",
                       "(6)","(7)","(8)","(9)","(10)")
Table_E13

### Print the log file for Table E13
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E13.log")
# log_print(Table_E13)
# log_close()

### Save Table E.13 as a Word document
# Table_E13_doc<-flextable(as.data.frame(Table_E13))
# save_as_docx(Table_E13_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E13.docx")

### Print Table E.14: negative factors age 42 and above
Table_E14<-func_table(c(out_negative_age[[3]][[1]],out_negative_age[[3]][[2]]))
colnames(Table_E14)<-c("","(1)","(2)","(3)","(4)","(5)",
                       "(6)","(7)","(8)","(9)","(10)")
Table_E14

### Print the log file for Table E14
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E14.log")
# log_print(Table_E14)
# log_close()

### Save Table E.14 as a Word document
# Table_E14_doc<-flextable(as.data.frame(Table_E14))
# save_as_docx(Table_E14_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E14.docx")

# Robustness support of other countries: Table E15 -----------------------

### support of development model in US and former colonial countries
dv_placebo<-c("BestModel_US","BestModel_Colonial")

### Estimate effects and output tables
out_main_other<-rep(list(rep(list(0),length(dv_placebo))),num_distance)
for (j in 1:num_distance){
  for (k in 1:length(dv_placebo)){
    
    out_main_other[[j]][[k]]<-func_model(data_respno[[j]],dv_placebo[k])
    
  }
}
out_main_other

### Print Table E.15: support for other countries
Table_E15<-func_table(c(out_main_other[[1]],out_main_other[[2]]))
colnames(Table_E15)<-c("","(1)","(2)","(3)","(4)")
Table_E15

### Print the log file for Table E15
# log_open("C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E15.log")
# log_print(Table_E15)
# log_close()

### Save Table E.15 as a Word document
# Table_E15_doc<-flextable(as.data.frame(Table_E15))
# save_as_docx(Table_E15_doc,path="C:/Users/xwang21/Dropbox/Research/China Model Africa/WD_R&R/Replication File WD/Table_E15.docx")






